package com.srit.system.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.srit.core.bean.Page;
import com.srit.core.sql.BaseResultSetExtractor;
import com.srit.core.sql.OrderBy;
import com.srit.forecast.bean.WeatherCode;
import com.srit.forecast.constant.Constant;
import com.srit.system.bean.Code;
import com.srit.system.dao.CodeDao;

@Service
@Transactional
public class CodeDaoImpl implements CodeDao {
	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public int getMaxId() {
		String sql = "select max(ID) FROM TC_CODE";
		return jdbcTemplate.queryForInt(sql);
	}

	@Override
	public void save(Code code) {
		String sql = "insert into TC_CODE (ID,NAME,CODE,CODETYPEID,REMARK,DELETED,ENABLED,IMAGE) values(SEQ_TC_CODE.nextval,?,?,?,?,?,?,?)";
		jdbcTemplate
				.update(sql,
						new Object[] { code.getName(), code.getCode(),
								code.getCodeTypeId(), code.getRemark(),
								code.getDeleted(), code.getEnabled(),
								code.getImage() });

	}

	@Override
	public void update(Code code) {
		String sql = "update TC_CODE set NAME=?,CODE=?,CODETYPEID=?,REMARK=?,DELETED=?,ENABLED=?,IMAGE=? WHERE ID=?";
		jdbcTemplate.update(
				sql,
				new Object[] { code.getName(), code.getCode(),
						code.getCodeTypeId(), code.getRemark(),
						code.getDeleted(), code.getEnabled(), code.getImage(),
						code.getId() });

	}

	@Override
	public Code getById(Integer id) {
		String sql = "select ID,NAME,CODE,CODETYPEID,REMARK,DELETED,ENABLED,IMAGE from TC_CODE WHERE ID=? ";
		return jdbcTemplate.queryForObject(sql, new Object[] { id },
				new CodeRowMapper());
	}

	@Override
	public void page(Page page) {
		String sqlCount = "select count(*) from TC_CODE where 1=1 ";
		String sqlAnd = " ";
		String sqlSelect = "select t2.ID,t2.NAME,t2.CODE,t2.CODETYPEID,t2.REMARK,t2.DELETED,t2.ENABLED,t2.IMAGE "
				+ "from (select rownum r,t1.* from TC_CODE t1 where rownum<? "
				+ sqlAnd + ") t2 where t2.r>?";
		int count = jdbcTemplate.queryForInt(sqlCount + sqlAnd);
		List<WeatherCode> list = jdbcTemplate.query(sqlSelect, new Object[] {
				(page.getPageNum() + 1) * page.getPageSize(),
				page.getPageNum() * page.getPageSize() }, new CodeRowMapper());
		page.setCount(count);
		page.setList(list);
	}

	@Override
	public void deleteSelect(List<Integer> ids) {
		List<Object[]> batchArgs = new ArrayList<Object[]>();
		for (Iterator<Integer> it = ids.iterator(); it.hasNext();) {
			Object[] objArr = new Object[] { Constant.DELETED, it.next() };
			batchArgs.add(objArr);
		}
		jdbcTemplate.batchUpdate("update TC_CODE set DELETED=? WHERE ID=?",
				batchArgs);
	}

	@Override
	public List<Code> listByCodeTypeId(int codeTypeId, OrderBy orderBy) {
		String sql = "select ID,NAME,CODE,CODETYPEID,REMARK,DELETED,ENABLED,IMAGE from TC_CODE WHERE CODETYPEID=? "
				+ orderBy;
		return jdbcTemplate.query(sql, new Object[] { codeTypeId },
				new CodeRowMapper());
	}

	@Override
	public void delete(Integer id) {
		// TODO Auto-generated method stub

	}

	@Override
	public List<Code> listByCodeType(String codeType, OrderBy orderBy) {
		String sql = "select ID,NAME,CODE,CODETYPEID,REMARK,DELETED,ENABLED,IMAGE from TC_CODE WHERE CODETYPEID=? "
				+ orderBy;
		return jdbcTemplate.query(sql, new Object[] { codeType },
				new CodeRowMapper());
	}

}

class CodeResultSetExtractor extends BaseResultSetExtractor<Code> {

}

class CodeRowMapper implements RowMapper {

	@Override
	public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
		CodeResultSetExtractor extractor = new CodeResultSetExtractor();
		return extractor.extractData(rs);
	}

}
